package com.vivz.baby;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.fluttercode.datafactory.impl.DataFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

import com.vivz.baby.dao.LogDAO;
import com.vivz.baby.dao.UserDAO;
import com.vivz.baby.domain.Log;
import com.vivz.baby.domain.LogType;
import com.vivz.baby.domain.User;

@EnableAutoConfiguration
@ComponentScan
public class AnnotationBasedDataLoader implements CommandLineRunner {

    private static final DataFactory DF = new DataFactory();

    @Autowired
    private LogDAO logDAO;

    @Autowired
    private UserDAO userDAO;

    public static void main(final String[] args) {
	SpringApplication.run(AnnotationBasedDataLoader.class, args);
    }

    @Override
    public void run(final String... args) throws Exception {
	// delete();
	// load();
	showLogs("user3");
    }

    private void showLogs(String string) {

    }

    public void delete() {

	final Calendar c = Calendar.getInstance();
	c.set(Calendar.YEAR, 2000);
	c.set(Calendar.MONTH, 1);
	c.set(Calendar.DATE, 1);

	final Date d1 = c.getTime();

	c.set(Calendar.YEAR, 2015);
	c.set(Calendar.MONTH, 1);
	c.set(Calendar.DATE, 1);
	final Date d2 = c.getTime();

	Long userId = null;
	logDAO.delete(logDAO.findByUserIdAndLogTimeBetween(userId, d1, d2));

	userDAO.deleteAll();
    }

    public final void load() {

	List<User> users = new ArrayList<User>();

	for (int i = 0; i < 5; i++) {
	    final User user = new User();
	    user.setEmail(DF.getEmailAddress());
	    user.setUsername(DF.getName().replaceAll(" ", "") + "_" + DF.getNumberBetween(10, 99));

	    User savedUser = userDAO.save(user);
	    System.out.println(ToStringBuilder.reflectionToString(savedUser, ToStringStyle.SHORT_PREFIX_STYLE));
	    users.add(savedUser);
	}

	final List<Integer> dates = new ArrayList<Integer>();
	for (int i = 1; i < 29; i++) {
	    dates.add(i);
	}

	final Calendar c = Calendar.getInstance();
	c.set(Calendar.YEAR, 2014);

	for (int month = Calendar.JANUARY; month <= Calendar.DECEMBER; month++) {

	    c.set(Calendar.MONTH, month);

	    for (final int date : dates) {

		c.set(Calendar.DATE, date);

		for (int hr = date % 7 + 1; hr <= c.getActualMaximum(Calendar.HOUR); hr += 3) {

		    c.set(Calendar.HOUR, hr);
		    c.set(Calendar.MINUTE, date * 3 % 60);

		    Log log = new Log();
		    log.setLogType(LogType.PUMPED);
		    log.setValue("" + (date % 2 == 0 ? 120 : 60));
		    log.setLogTime(c.getTime());
		    setDefaults(log);
		    logDAO.save(log);

		    log = new Log();
		    log.setLogType(LogType.RIGHT_BREAST);
		    log.setValue("" + (date % 2 == 0 ? 120 : 60));
		    log.setLogTime(c.getTime());
		    setDefaults(log);
		    logDAO.save(log);

		    log = new Log();
		    log.setLogType(LogType.LEFT_BREAST);
		    log.setValue("" + (date % 2 == 0 ? 120 : 60));
		    log.setLogTime(c.getTime());
		    setDefaults(log);
		    logDAO.save(log);

		    log = new Log();
		    log.setLogType(LogType.FORMULA);
		    log.setValue("" + (date % 2 == 0 ? 120 : 60));
		    log.setLogTime(c.getTime());
		    setDefaults(log);
		    logDAO.save(log);

		    log = new Log();
		    log.setLogType(LogType.DIAPERS);
		    log.setValue(date % 2 == 0 ? "Poo Poo" : "Pee Pee");
		    log.setLogTime(c.getTime());
		    setDefaults(log);
		    logDAO.save(log);
		}
	    }
	}
    }

    private void setDefaults(Log log) {
	log.setCreateTimestamp(new Date());
	log.setCreateUser(String.valueOf(AnnotationBasedDataLoader.class));
	log.setUpdateTimestamp(new Date());
	log.setUpdateUser(String.valueOf(AnnotationBasedDataLoader.class));
    }

}
